package io.renren.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.annotation.Login;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.entity.*;
import io.renren.form.LoginForm;
import io.renren.service.SysMerchantService;
import io.renren.service.SysWithdrawConfigService;
import io.renren.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;


/**
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-10-12 16:39:41
 */
@RestController
@RequestMapping("api/sysmerchant")
@Api(tags = "商户")
public class SysMerchantController {
    @Autowired
    private SysMerchantService sysMerchantService;

    @Autowired
    private SysWithdrawConfigService sysWithdrawConfigService;
    @Autowired
    private TokenService tokenService;

    @PostMapping("login")
    @ApiOperation("登录")
    public R login(@RequestBody LoginForm form){
        //表单校验
        ValidatorUtils.validateEntity(form);

        //用户登录
        Map<String, Object> map = sysMerchantService.login(form);

        return R.ok(map);
    }

    @Login
    @PostMapping("logout")
    @ApiOperation("退出")
    public R logout(@ApiIgnore @RequestAttribute("userId") long userId){
        tokenService.expireToken(userId);
        return R.ok();
    }

//    /**
//     * 列表
//     */
//    @GetMapping("/list")
//    @ApiOperation("列表")
//    public R list(@RequestParam Map<String, Object> params){
//        PageUtils page = sysMerchantService.queryPage(params);
//
//        return R.ok().put("page", page);
//    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("根据ID查询商户信息")
    public R info(@PathVariable("id") Integer id){
        SysMerchantEntity sysMerchant = sysMerchantService.getById(id);
        //最低提现金额
        SysWithdrawConfigEntity one = sysWithdrawConfigService.getOne(new QueryWrapper<SysWithdrawConfigEntity>());
        BigDecimal merchantMin = one.getMerchantMin();
        sysMerchant.setMin(merchantMin);
        return R.ok().put("sysMerchant", sysMerchant);
    }

    /**
     * 根据token查询商户信息
     */
    @GetMapping("/info")
    @ApiOperation("根据token查询商户信息")
    public R queryInfo(@RequestParam String token) {
        Map<String, Object> map = new HashMap<>();
        String[] roles = new String[]{"admin"};
        map.put("roles", roles);
        map.put("name", "管理员");
        map.put("avatar", "https://ruyizhou.oss-cn-zhangjiakou.aliyuncs.com/20201105/bf19e1d29bd840e8a270f590605479fd.jpg");
        map.put("introduction", "管理员");
        return R.ok().put("data", map);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("添加")
    public R save(@RequestBody SysMerchantEntity sysMerchant){
        ValidatorUtils.validateEntity(sysMerchant);
        sysMerchant.setPassword(DigestUtils.md5Hex(sysMerchant.getPassword()));
        sysMerchantService.save(sysMerchant);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/ud")
    @ApiOperation("修改基本信息")
    public R update(@RequestBody SysMerchantEntity sysMerchant){
        Integer id = sysMerchant.getId();
        SysMerchantEntity oldMerchantEntity = sysMerchantService.getById(id);
        oldMerchantEntity.setBank(sysMerchant.getBank());
        oldMerchantEntity.setCard(sysMerchant.getCard());
        oldMerchantEntity.setMobile(sysMerchant.getMobile());
        oldMerchantEntity.setUserName(sysMerchant.getUserName());
        oldMerchantEntity.setName(sysMerchant.getName());
        sysMerchantService.updateById(oldMerchantEntity);
        return R.ok();
    }
    /**
     * 修改
     */
    @PostMapping("/udpass")
    @ApiOperation("修改密码")
    public R update(@RequestBody Map<String,String> params){
        sysMerchantService.updatePassword(params);
        return R.ok();
    }

    /**
     * 信息
     */
    @GetMapping("/cashout/{id}")
    @ApiOperation("商户提现")
    public R cashout(@PathVariable("id") Integer id,@RequestParam("cash") BigDecimal cash){
       sysMerchantService.cashOut(id,cash);
        return R.ok("申请已提交,请耐心等候");
    }


    /**
     * 根据ID查看商户所有产品
     */
    @GetMapping("/products/{merchantId}")
    @ApiOperation("根据商户ID查看所有产品")
    public R queryProducts(@PathVariable Integer merchantId, @RequestParam Map<String, Object> params) {

        PageUtils pageUtils = sysMerchantService.queryProducts(merchantId, params);
        return R.ok().put("page", pageUtils);
    }

//    /**
//     * 快递费用设置
//     */
//    @PostMapping("/updateCourierFee")
//    @ApiOperation("快递费用设置")
//    public R updateCourierFee(Integer merchantId, BigDecimal money) {
//        sysMerchantService.updateCourierFee(merchantId, money);
//        return  R.ok();
//    }

    /**
     * 买家付款 库存变
     */
    @PostMapping("/reduceInventories")
    public R reduceInventories(@RequestBody Map<String, Object> map) {
        if (!map.containsKey("id") || !map.containsKey("inventories")) {
            throw new RRException("传参错误");
        }
        Integer id = (Integer) map.get("id");
        Integer inventories = (Integer) map.get("inventories");
        if (id == null || inventories == null) {
            throw new RRException("传参错误");
        }
        sysMerchantService.reduceInventories(id, inventories);
        return R.ok();
    }
    /**
     * 买家确认收货 冻结返利
     */
    /**
     * 用户点击确定收货
     */
    @PostMapping("/memberConfirmReceipt")
    @ApiOperation("用户点击确认收货")
    public R memberConfirmReceipt(@RequestBody Map<String, Object> map) {
        sysMerchantService.memberConfirmReceipt(map);
        return R.ok();
    }

    /**
     * 商户点击确认收货：退货
     */
    @PostMapping("/merchantConfirmReceipt")
    @ApiOperation("商户点击确认收货")
    public R merchantConfirmReceipt(@RequestBody Map<String, Object> map) {
        sysMerchantService.merchantConfirmReceipt(map);
        return R.ok();
    }
    /**
     * 付款 未发货
     * 买家申请退款
     * 同意退款
     * 库存变 资金变
     */
    /**
     * 同意退款
     */
    @PostMapping("/agreeRefund")
    @ApiOperation("卖家同意退款")
    public R agreeRefund(@RequestBody Map<String, Object> map) {
        sysMerchantService.agreeRefund(map);

        return R.ok();
    }


    /**
     * 付款 已发货
     * 买家未确认收货
     * 买家申请退款
     * 卖家确认收货 库存变
     * 同意退款
     * 买家确认收货
     * 买家申请退货
     * 同意退货
     * 确认收货 库存变
     * 申请退款
     * 同意退款 资金变 冻结表定时任务取消
     */


    /**
     * 根据skuId 修改该商品的库存
     */

    @RequestMapping("/updateStock")
    public R updateBySkuId(@RequestBody SysProductSkuEntity skuEntity){
        //查询该商品的
        sysMerchantService.updateStock(skuEntity);
        return R.ok();
    }

}
